﻿using DataBase.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Orleans;
using Orleans.Hosting;
using Server.Impl;

namespace Server
{
    /// <summary>
    /// 测试服务，可以做成winform，由thread装载
    /// 别用IIS
    /// </summary>
    internal class Program
    {

        static async Task Main(string[] args)
        {
            Console.WriteLine("***************  服务开始启动   ***********!");
            await RunMainAsync();
            Console.WriteLine("服务结束");

        }


        static async Task<int> RunMainAsync()
        { 
            try
            {
                //启动silo
                var host = await StartSilo();
                Console.WriteLine("\n\n 输入 exit 并回车，结束程序\n\n");

                //循环，保存程序不死。
                string? input = "";
                while (true)
                {
                    input = Console.ReadLine();
                    if (input == "exit")
                    {
                        Console.WriteLine("开始启动结束流程,等待资源释放");
                        break;
                    }
                }
                await host.StopAsync();
                Console.WriteLine("资源已释放");
                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return 1;
            }
        }

        static async Task<ISiloHost> StartSilo()
        {
#if DEBUG
            string redis = "192.168.3.132:6379";
#else
            string redis = "127.0.0.1:6379";
#endif

            string dbstr = "";  //数据库连接字符串

            var builder = new SiloHostBuilder()
                .UseLocalhostClustering()
                .ConfigureApplicationParts(parts =>
                {
                    parts.AddApplicationPart(typeof(AgvImpl).Assembly).WithReferences();
                    parts.AddApplicationPart(typeof(AgvSetImpl).Assembly).WithReferences();
                    parts.AddApplicationPart(typeof(AreaLockImpl).Assembly).WithReferences();
                    parts.AddApplicationPart(typeof(AreaSetImpl).Assembly).WithReferences();
                    parts.AddApplicationPart(typeof(MarkerSetImpl).Assembly).WithReferences();
                    parts.AddApplicationPart(typeof(RouteImpl).Assembly).WithReferences();
                    parts.AddApplicationPart(typeof(StayGroupImpl).Assembly).WithReferences();

                })
               .ConfigureServices(service =>
               {
                   service.AddDbContext<ApplicationDBContext>(options =>
                   { 
                       options.UseMySql(dbstr, new MySqlServerVersion(new Version(8, 0, 11)));
                   }); 
               })
               .AddRedisGrainStorage("redisStorage", options =>
               {
                   options.ConnectionString = redis;
                   options.DatabaseNumber = 4;
                   options.UseJson = true;
                   options.DeleteOnClear = true;
               })
                .AddSimpleMessageStreamProvider("DeviceProvider", options =>
                {
                    options.FireAndForgetDelivery = true;
                }) 
                 .AddGrainService<ListenerService>()  //侦听服务
                .ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Error).AddConsole());
            var host = builder.Build();

            await host.StartAsync();
            return host;
        }



        
    }
}